<?php
function parserUrl() {
	$baseUrl = $_GET['__baseurl'];
	$request_uri = $_SERVER['REQUEST_URI'];

	$request_uri = parse_url($request_uri, PHP_URL_PATH);
	$url = substr($request_uri, strlen($baseUrl));
	$url = trim($url, '/');

	$controller = 'Index';	// default controller
	$action = '';
	if($url) {
		$urls = explode("/", $url);

		$controller = $urls[0];
		array_shift($urls);
		$action = $urls[0];
		array_shift($urls);

		for ($i = 0; $i < count($urls) &&  $i<10; $i++) {
			$_REQUEST[$urls[$i]] = $urls[++$i];
		}
	}

	require_once 'Dispatcher.php';
	$dispatcher = new Dispatcher();
	$dispatcher->setController($controller);
	$dispatcher->setAction($action);
	$dispatcher->setBaseUrl($baseUrl);
	$dispatcher->dispatch();
}

try {
	parserUrl();
} catch (PDOException $e) {
	var_dump('PDOException occured!');
	//print_r($e);
} catch (SmartyException $e) {
	var_dump('Smarty Exception occured!');
	echo $e;
} catch (Exception $e) {
	var_dump('PHP Exception occured!');
	var_dump($e);
}